﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Facilities.TypedFactory;
using System.Reflection;
using Castle.MicroKernel.Registration;
using System.Web.Mvc;
using MoleCart.Core.Data;
using MoleCart.Data;
using MoleCart.Core.Service.Catalog;
using MoleCart.Service.Catalog;
using MoleCart.Core.Domain.Catalog;
using MoleCart.Core.Data.Catalog;
using MoleCart.Data.Repository;

namespace MoleCart.Web.Ioc {
    public static class ContainerBuilder {
        public static IWindsorContainer Build(string configPath) {
            var container = new WindsorContainer(new XmlInterpreter(configPath));
            // typed factory facility
            container.AddFacility<TypedFactoryFacility>();

            // automatically register controllers
            container.Register(Classes
                                   .FromAssembly(Assembly.GetExecutingAssembly())
                                   .BasedOn<IController>()
                                   .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower())));

            //container.Register(
            //    Component.For(typeof(IRepository<>)).ImplementedBy(typeof(RepositoryBase<>)).LifeStyle.Transient
            //    //Component.For(typeof(IProductRepository)).ImplementedBy(typeof(ProductRepository)).LifeStyle.Transient
            //    //Component.For(typeof(IProductService)).ImplementedBy(typeof(ProductService)).LifeStyle.Transient
            //    );
            return container;
        }

        public static DynamicParametersDelegate ProductP { get; set; }
    }
}